package com.cn.web.controller.bc;

import com.alibaba.fastjson2.JSONObject;

import com.cn.bean.pojo.bc.Mater;
import com.cn.bean.pojo.bc.MaterList;
import com.cn.bean.pojo.mc.K3Dy;
import com.cn.bean.pojo.mc.K3Fl;
import com.cn.bean.vo.bc.MateVo;
import com.cn.config.config.Msg;
import com.cn.mapper.dao.db1.bc.Mate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@CrossOrigin
@RestController
public class MateController{
    private final Mate mate;


    @Autowired
    public MateController(Mate mate) {
        this.mate = mate;

    }

    @RequestMapping("/select/Purposes/B")
    @ResponseBody
    public MateVo selectPurposes(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String userName = jsonObject.getString("userName");
        //查看访问权限
        MateVo mateVo = new MateVo();

        List<Mater> useClassifies = mate.selectUseClassify();
        List<MaterList> useDefinitions = mate.pagination(limit, page);
        List<MaterList> downDefinitionList = mate.selectUseDefinition();
        int definitionCount = mate.getDefinitionCount();
        mateVo.setDefinitionsList(useDefinitions);
        mateVo.setDownDefinitionList(downDefinitionList);
        mateVo.setClassifyList(useClassifies);
        mateVo.setTotal(definitionCount);

        return mateVo;
    }

    // 分页
    @RequestMapping("/select/Pagination/B")
    @ResponseBody
    public MateVo selectPagination(@RequestBody JSONObject jsonObject) {

        Integer limit = jsonObject.getInteger("limit");
        Integer page = jsonObject.getInteger("page");
        String flmc = jsonObject.getString("flmc");
        MateVo mateVo = new MateVo();
        String userName = jsonObject.getString("userName");
        //查看访问权限
        if (flmc.equals("材质分类")) {

            List<MaterList> pagination = mate.pagination(limit, page);
            int total = pagination.get(0).getTotal();
            mateVo.setTotal(total);
            mateVo.setDefinitionsList(pagination);
            return mateVo;
        }
        int total = 0;
        List<MaterList> useDefinitions = mate.searchPagination(limit, page, "", "", flmc);
        if (!useDefinitions.isEmpty()) {
            total = useDefinitions.get(0).getTotal();
        }
        mateVo.setTotal(total);
        mateVo.setDefinitionsList(useDefinitions);
        return mateVo;
    }

    //搜索框
    @RequestMapping("/search/Pagination/B")
    @ResponseBody
    public MateVo searchPagination(@RequestBody JSONObject jsonObject) {
        int limit = jsonObject.getInteger("limit");
        int page = jsonObject.getInteger("page");
        String ytbm = jsonObject.getString("inputBm");
        String ytmc = jsonObject.getString("inputMc");
        String userName = jsonObject.getString("userName");
        MateVo mateVo = new MateVo();
        int total = 0;
        ytmc = (ytmc != null) ? ytmc : "";
        ytbm = (ytbm != null) ? ytbm : "";
        List<MaterList> useDefinitions = mate.searchPagination(limit, page, ytmc, ytbm, "");

        if (!useDefinitions.isEmpty()) {
            total = useDefinitions.get(0).getTotal();
        }
        mateVo.setTotal(total);
        mateVo.setDefinitionsList(useDefinitions);
        return mateVo;
    }

    //   点击侧边栏触发事件
    @RequestMapping("/select/Definition/flmc/B")
    @ResponseBody
    public MateVo selectUseDefinitionFlmc(@RequestBody JSONObject jsonObject) {
        int limit = jsonObject.getInteger("limit");
        int page = jsonObject.getInteger("page");
        Integer id = jsonObject.getInteger("id");
        String ytbm = "";
        String ytmc = "";
        String flmc = jsonObject.getString("flmc");
        int total = 0;
        MateVo mateVo = new MateVo();
        if (id==1) {
            List<MaterList> useDefinitions = mate.pagination(limit, page);
            total = useDefinitions.get(0).getTotal();
            mateVo.setTotal(total);
            mateVo.setDefinitionsList(useDefinitions);
            return mateVo;
        }
        //当前分类
        Mater mat = mate.selectId(id);
        //所有分类
        List<Mater> maters = mate.selectUseClassify();
        //所有定义
        List<MaterList> materLists = mate.selectMaterListAll();
        List<Mater> subcategories = getSubcategories(maters, id);
        List<MaterList> categorizedDys = getCategorizedDys(materLists, subcategories);
        mateVo.setUseClassify(mat);
        mateVo.setDefinitionsList(categorizedDys);
        return mateVo;
    }
    private List<MaterList> getCategorizedDys(List<MaterList> allDys, List<Mater> categories) {
        List<MaterList> categorizedDys = new ArrayList<>();

        for (MaterList dy : allDys) {
            for (Mater category : categories) {
                if (dy.getFq() == category.getId()) {
                    categorizedDys.add(dy);
                    break;  // 如果匹配到当前分类，就不需要再继续查找
                }
            }
        }

        return categorizedDys;
    }
    private List<Mater> getSubcategories(List<Mater> allCategories, int id) {
        List<Mater> subcategories = new ArrayList<>();

        for (Mater category : allCategories) {
            if (category.getParentBh() == id) {
                subcategories.add(category);
                List<Mater> children = getSubcategories(allCategories, category.getId());
                subcategories.addAll(children);
            } else if (category.getId() == id) {
                // 如果当前分类就是目标分类，也将其添加到列表中
                subcategories.add(category);
            }
        }
        return subcategories;
    }

    //  添加分类
    @RequestMapping("/insert/Classify/B")
    @ResponseBody
    public Msg insertClassify(@RequestBody JSONObject jsonObject) {
        String flmc = jsonObject.getString("flmc");
        int state = jsonObject.getIntValue("state");
        String remark = jsonObject.getString("remark");
        //上级分类
//        String parentCategory = jsonObject.getString("parentCategory");
        Integer parentCategory = jsonObject.getInteger("parentCategory");
        Msg msg = new Msg();
        String userName = jsonObject.getString("userName");


        int ytflbh = mate.getClassifyCount() + 1;

        if (parentCategory==null) {
            Integer result = mate.repeatFlmc(flmc);
            if (result != null && result > 0) {
                msg.setCode(500);
                msg.setMessage("分类名称在同一层级名称重复");
                return msg;
            }
            int isOK = mate.insertClassify(ytflbh, flmc, state, remark, 1, 1, 0);
            if (isOK > 0) {
                msg.setCode(200);
                msg.setMessage("添加成功");
                return msg;
            }
            msg.setCode(500);
            msg.setMessage("添加失败");
            return msg;
        }
//        Mater useClassify = mate.selectParentId(parentCategory);
        Mater useClassify = mate.selectMaterId(parentCategory);
        int level = useClassify.getLevel() + 1;
        Integer ifRepeat = mate.repeatFlmcLevel(flmc, level);
        if (ifRepeat != null && ifRepeat > 0) {
            msg.setCode(500);
            msg.setMessage("分类名称在同一层级名称重复");
            return msg;
        }
        int parentBh = useClassify.getYtflbh();
        int i1 = mate.insertClassify(ytflbh, flmc, state, remark, level, parentBh, 0);
        if (i1 > 0) {
            msg.setCode(200);
            msg.setMessage("添加成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("添加失败");
        return msg;
    }

    //删除分类
    @RequestMapping("/delete/Classify/B")
    @ResponseBody
    public Msg deleteClassify(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        int ytflbh = jsonObject.getInteger("ytflbh");
        System.out.println("进入页面");
        System.out.println(ytflbh);
        //判断删除的权限
        String userName = jsonObject.getString("userName");
        //删除分类前验证，是否拥有子节点，有就无法删除
        Integer i1 = mate.repeatClassifyParentBh(ytflbh);
        System.out.println("查看i1删除:"+i1);
        if (i1 > 0) {
            msg.setCode(500);
            msg.setMessage("请先删除这个分类的子节点");
            return msg;
        }
        int i = mate.deleteClassify(String.valueOf(ytflbh));
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("删除成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("删除失败");
        return msg;
    }

    //添加用途
    @RequestMapping("/insert/Definition/B")
    @ResponseBody
    public Msg insertUseDefinition(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
//        String ytfl = jsonObject.getString("ytfl");
        Integer ytfl = jsonObject.getInteger("ytfl");
        String ytmc = jsonObject.getString("ytmc");
        int state = jsonObject.getIntValue("state");
        String remark = jsonObject.getString("remark");
        String czzm = jsonObject.getString("czzm");
        String md = jsonObject.getString("md");
        String sbm = jsonObject.getString("sbm");
        //添加用途的权限
        String userName = jsonObject.getString("userName");
        int ytbm = mate.getDefinitionCount() + 1;
        //上级分类
        Mater useClassify = mate.selectMaterId(ytfl);
//        Mater useClassify = mate.selectYtfl(ytfl);
        int fq = useClassify.getId();
        int isOK = mate.insertUseDefinition(ytbm, useClassify.getFlmc(), ytmc, state, remark, 0, fq, 0, czzm,md,sbm,1);
        if (isOK > 0) {
            msg.setCode(200);
            msg.setMessage("添加成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("添加失败");
        return msg;
    }

    //修改用途
    @RequestMapping("/update/Definition/B")
    @ResponseBody
    public Msg updateUseDefinition(@RequestBody JSONObject jsonObject) {
        int ytbm = jsonObject.getInteger("ytbm");
//        String ytfl = jsonObject.getString("ytfl");
        Integer ytfl = jsonObject.getInteger("ytfl");
        String ytmc = jsonObject.getString("ytmc");
        int state = jsonObject.getInteger("state");
        String remark = jsonObject.getString("bz");
        String czzm = jsonObject.getString("czzm");
        String md = jsonObject.getString("md");
        String sbm = jsonObject.getString("sbm");
        Msg msg = new Msg();
        //修改用途的权限
        String userName = jsonObject.getString("userName");
        Mater useClassify = mate.selectMaterId(ytfl);
//        Mater useClassify = mate.selectYtfl(ytfl);
        int fq = useClassify.getId();
        int i = mate.updateUseDefinition(ytbm, useClassify.getFlmc(), ytmc, state, remark,czzm,md,sbm, fq);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("修改成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("修改失败");
        return msg;
    }

    @RequestMapping("/delete/Definition/B")
    @ResponseBody
    public Msg deleteDefinition(@RequestBody JSONObject jsonObject) {
        String ytmc = jsonObject.getString("ytmc");
        Msg msg=new Msg();
        String userName = jsonObject.getString("userName");
        int i = mate.deleteDefinition(ytmc);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("删除成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("删除失败");
        return msg;
    }

    @RequestMapping("/update/State0/B")
    @ResponseBody
    public Msg updateState0(@RequestBody JSONObject jsonObject) {
        String ytmc = jsonObject.getString("ytmc");
        Msg msg = new Msg();
        int i = mate.updateState0(ytmc);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("审核成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("审核失败!");
        return msg;
    }

    @RequestMapping("/update/State1/B")
    @ResponseBody
    public Msg updateState1(@RequestBody JSONObject jsonObject) {
        String ytmc = jsonObject.getString("ytmc");
        Msg msg = new Msg();
        int i = mate.updateState1(ytmc);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("弃审成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("弃审失败!");
        return msg;
    }

    //修改分类
    @RequestMapping("/update/Classify/B")
    @ResponseBody
    public Msg updateClassify(@RequestBody JSONObject jsonObject) {
        int ytflbh = jsonObject.getInteger("ytflbh");
        String flmc = jsonObject.getString("flmc");
        int state = jsonObject.getInteger("state");
        String remark = jsonObject.getString("remark");
//        String ytfl = jsonObject.getString("ytfl");
        Integer ytfl = jsonObject.getInteger("ytfl");
        //验证flmc有没有修改
        Mater selectUseClassifyId = mate.selectUseClassifyId(ytflbh);
        String flmc1 = selectUseClassifyId.getFlmc();
        Msg msg = new Msg();
        String userName = jsonObject.getString("userName");
        if (flmc.equals(flmc1)) {
            if (ytfl!=null) {
                Mater useClassify = mate.selectMaterId(ytfl);
//                Mater useClassify = mate.selectParentId(ytfl);
                int level = useClassify.getLevel() + 1;
                int id = useClassify.getYtflbh();
                int i = mate.updateClassify(ytflbh, flmc, state, remark, level, id);
                if (i > 0) {
                    msg.setCode(200);
                    msg.setMessage("修改成功");
                    return msg;
                }
                msg.setCode(500);
                msg.setMessage("修改失败");
            }
            int i = mate.updateClfy(ytflbh, flmc, state, remark);
            if (i > 0) {
                msg.setCode(200);
                msg.setMessage("修改成功");
                return msg;
            }
            msg.setCode(500);
            msg.setMessage("修改失败");
            return msg;
        }
        if (ytfl==null) {
            Integer ifRepeat = mate.repeatFlmcLevel(flmc, 1);
            if (ifRepeat > 0) {
                msg.setCode(500);
                msg.setMessage("分类名称在同一层级名称重复");
                return msg;
            }
            int i = mate.updateClfy(ytflbh, flmc, state, remark);
            if (i > 0) {
                msg.setCode(200);
                msg.setMessage("修改成功");
                return msg;
            }
            msg.setCode(500);
            msg.setMessage("修改失败");
            return msg;
        }
//        Mater useClassify = mate.selectParentId(ytfl);
        Mater useClassify = mate.selectMaterId(ytfl);
        Integer ifRepeat = mate.repeatFlmcLevel(flmc, useClassify.getLevel()+1);
        if (ifRepeat > 0) {
            msg.setCode(500);
            msg.setMessage("分类名称在同一层级名称重复");
            return msg;
        }
        int level = useClassify.getLevel() + 1;
        int id = useClassify.getYtflbh();
        int i = mate.updateClassify(ytflbh, flmc, state, remark, level, id);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("修改成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("修改失败");
        return msg;
    }
}
